# -*- coding: utf-8 -*-
'''
Created on 2018.06.25
@author: wuyou
'''

import pytest
import os
import copy
import random
import datetime
import json
import itertools
from MyLibrary.ReadWriteExcel import ReadWriteExcel
from MyLibrary.OperaLib import OperaLib
from MyLibrary.CreateData import CreateData
from multiprocessing.dummy import Pool as ThreadPool
excel = ReadWriteExcel()
opera = OperaLib()
createdata = CreateData()

#当前路径: \Real_Time_test\TestCase
casepath = os.path.dirname(__file__)
#Base路径: \Real_Time_test
basepath = os.path.dirname(casepath)
#Config file: \Real_Time_test\Config\Configuration.xlsx
configfile = os.path.join(basepath + "\Config\\","Configuration.xlsx")
config_sheet = 'env_config'

#测试数据：\Real_Time_test\TestData\testdata.xlsx
testfile = os.path.join(basepath + "\TestData\\","testdata.xlsx")

class get_excel_data():

    def __init__(self):
        end = excel.get_excel_row_count(configfile, config_sheet)
        configdict=excel.get_excel_name_value_dict(configfile, config_sheet,'B','C','2',end)
        print(configdict)
        self.parasheet = configdict['parasheet']
        self.SendCacheurl = configdict['SendCacheurl']
        self.Requesturl = configdict['Requesturl']
        self.Requesturl9680 = configdict['Requesturl9680']
        self.Requesturl9880 = configdict['Requesturl9880']
        self.Requesturl9190 = configdict['Requesturl9190']
        self.DelCacheurl = configdict['DelCacheurl']

        #各种列名
        self.para_name_col = configdict['para_name_col']
        self.para_value_col = configdict['para_value_col']
        self.change_re_para_col = configdict['change_re_para_col']
        self.cache_para_col = configdict['cache_para_col']
        self.expect_result_col = configdict['expect_result_col']
        self.expect_result_col = configdict['expect_result_col']
        self.case_name_col = configdict['case_name_col']
        self.key_col = configdict['key_col']
        self.table_col = configdict['table_col']

        #注册sheet页，case起止行
        self.zc_case_sheet = configdict['zc_case_sheet']
        self.zc_case_start = configdict['zc_case_start']
        self.zc_case_end = configdict['zc_case_end']
        #登陆sheet页，case起止行
        self.dl_case_sheet = configdict['dl_case_sheet']
        self.dl_case_start = configdict['dl_case_start']
        self.dl_case_end = configdict['dl_case_end']
        #交易
        self.jy_case_sheet = configdict['jy_case_sheet']
        self.jy_case_start = configdict['jy_case_start']
        self.jy_case_end = configdict['jy_case_end']
        #激活
        self.jh_case_sheet = configdict['jh_case_sheet']
        self.jh_case_start = configdict['jh_case_start']
        self.jh_case_end = configdict['jh_case_end']
        #绑卡
        self.bk_case_sheet = configdict['bk_case_sheet']
        self.bk_case_start = configdict['bk_case_start']
        self.bk_case_end = configdict['bk_case_end']
        #商城反欺诈
        self.scfqz_case_sheet = configdict['scfqz_case_sheet']
        self.scfqz_case_start = configdict['scfqz_case_start']
        self.scfqz_case_end = configdict['scfqz_case_end']
        #扫码支付
        self.smzf_case_sheet = configdict['smzf_case_sheet']
        self.smzf_case_start = configdict['smzf_case_start']
        self.smzf_case_end = configdict['smzf_case_end']
        #通用
        self.ty_case_sheet = configdict['ty_case_sheet']
        self.ty_case_start = configdict['ty_case_start']
        self.ty_case_end = configdict['ty_case_end']
        #公共
        self.gg_case_sheet = configdict['gg_case_sheet']
        self.gg_case_start = configdict['gg_case_start']
        self.gg_case_end = configdict['gg_case_end']
        #黑名单_客户评级B
        self.phhmdB_case_sheet = configdict['phhmdB_case_sheet']
        self.phhmdB_case_start = configdict['phhmdB_case_start']
        self.phhmdB_case_end = configdict['phhmdB_case_end']
        #黑名单_客户评级非B
        self.phhmdO_case_sheet = configdict['phhmdO_case_sheet']
        self.phhmdO_case_start = configdict['phhmdO_case_start']
        self.phhmdO_case_end = configdict['phhmdO_case_end']
        #黑名单_无客户评级
        self.phhmd_case_sheet = configdict['phhmd_case_sheet']
        self.phhmd_case_start = configdict['phhmd_case_start']
        self.phhmd_case_end = configdict['phhmd_case_end']
        #普惠贷前
        self.phdq_case_sheet = configdict['phdq_case_sheet']
        self.phdq_case_start = configdict['phdq_case_start']
        self.phdq_case_end = configdict['phdq_case_end']
        #普惠贷中
        self.phdz_case_sheet = configdict['phdz_case_sheet']
        self.phdz_case_start = configdict['phdz_case_start']
        self.phdz_case_end = configdict['phdz_case_end']
        #SAAS-交易
        self.saas_jy_case_sheet = configdict['saas_jy_case_sheet']
        self.saas_jy_case_start = configdict['saas_jy_case_start']
        self.saas_jy_case_end = configdict['saas_jy_case_end']
        #SAAS-登陆
        self.saas_dl_case_sheet = configdict['saas_dl_case_sheet']
        self.saas_dl_case_start = configdict['saas_dl_case_start']
        self.saas_dl_case_end = configdict['saas_dl_case_end']
        #SAAS-注册
        self.saas_zc_case_sheet = configdict['saas_zc_case_sheet']
        self.saas_zc_case_start = configdict['saas_zc_case_start']
        self.saas_zc_case_end = configdict['saas_zc_case_end']
        #SAAS-绑卡
        self.saas_bk_case_sheet = configdict['saas_bk_case_sheet']
        self.saas_bk_case_start = configdict['saas_bk_case_start']
        self.saas_bk_case_end = configdict['saas_bk_case_end']
        #SAAS-激活
        self.saas_jh_case_sheet = configdict['saas_jh_case_sheet']
        self.saas_jh_case_start = configdict['saas_jh_case_start']
        self.saas_jh_case_end = configdict['saas_jh_case_end']
        #SAAS-通用
        self.saas_ty_case_sheet = configdict['saas_ty_case_sheet']
        self.saas_ty_case_start = configdict['saas_ty_case_start']
        self.saas_ty_case_end = configdict['saas_ty_case_end']

        #猛犸-交易
        self.ma_jy_case_sheet = configdict['ma_jy_case_sheet']
        self.ma_jy_case_start = configdict['ma_jy_case_start']
        self.ma_jy_case_end = configdict['ma_jy_case_end']
        #猛犸-登陆
        self.ma_dl_case_sheet = configdict['ma_dl_case_sheet']
        self.ma_dl_case_start = configdict['ma_dl_case_start']
        self.ma_dl_case_end = configdict['ma_dl_case_end']
        #猛犸-注册
        self.ma_zc_case_sheet = configdict['ma_zc_case_sheet']
        self.ma_zc_case_start = configdict['ma_zc_case_start']
        self.ma_zc_case_end = configdict['ma_zc_case_end']
        #猛犸-绑卡
        self.ma_bk_case_sheet = configdict['ma_bk_case_sheet']
        self.ma_bk_case_start = configdict['ma_bk_case_start']
        self.ma_bk_case_end = configdict['ma_bk_case_end']
        #猛犸-激活
        self.ma_jh_case_sheet = configdict['ma_jh_case_sheet']
        self.ma_jh_case_start = configdict['ma_jh_case_start']
        self.ma_jh_case_end = configdict['ma_jh_case_end']
        #猛犸-通用
        self.ma_ty_case_sheet = configdict['ma_ty_case_sheet']
        self.ma_ty_case_start = configdict['ma_ty_case_start']
        self.ma_ty_case_end = configdict['ma_ty_case_end']

    #Config excel读取request默认入参名和值，拼接成字典,生成1条request 的 base数据
    def gen_one_re_data(self):
        end = excel.get_excel_row_count(configfile, self.parasheet)
        re_base_data = excel.get_excel_name_value_dict(configfile, self.parasheet,self.para_name_col,self.para_value_col,'2',end)

        #部分参数处理成int型和float型
        int_list = ['frms_oper_time','frms_trans_vol']
        float_list = ['frms_longitude','frms_dimension']
        for key in float_list:
            re_base_data[key]=float(re_base_data[key])
        for key in int_list:
            re_base_data[key]=int(re_base_data[key])

        return re_base_data


    #根据rechangelist，对request base data 进行变更
    def gen_re_data_list(self,sheet,test_start,test_end):
        #获取变更入参
        re_change_list = excel.colname_read_excel_return_list(testfile,sheet,\
                     self.change_re_para_col,test_start, test_end)

        re_data_list = []
        #将变更入参更新到request中，并返回request list
        for i in range(len(re_change_list)):
            re_change_dict = opera.transform_data(str(re_change_list[i]))
            re_data_dict = self.gen_one_re_data()
            re_data_dict = opera.update_dict(re_change_dict,re_data_dict)
            re_data_list.append(re_data_dict)
        return re_data_list

    #根据type选择对应的用例sheet页和起止行
    def choose_sheet(self,testtype):
        if testtype=='注册':
            sheet = self.zc_case_sheet
            test_start = self.zc_case_start
            test_end = self.zc_case_end
        elif testtype=='登陆':
            sheet = self.dl_case_sheet
            test_start = self.dl_case_start
            test_end = self.dl_case_end
        elif testtype=='交易':
            sheet = self.jy_case_sheet
            test_start = self.jy_case_start
            test_end = self.jy_case_end
        elif testtype=='激活':
            sheet = self.jh_case_sheet
            test_start = self.jh_case_start
            test_end = self.jh_case_end
        elif testtype=='绑卡':
            sheet = self.bk_case_sheet
            test_start = self.bk_case_start
            test_end = self.bk_case_end
        elif testtype=='商城反欺诈':
            sheet = self.scfqz_case_sheet
            test_start = self.scfqz_case_start
            test_end = self.scfqz_case_end
        elif testtype=='扫码支付':
            sheet = self.smzf_case_sheet
            test_start = self.smzf_case_start
            test_end = self.smzf_case_end
        elif testtype=='通用':
            sheet = self.ty_case_sheet
            test_start = self.ty_case_start
            test_end = self.ty_case_end
        elif testtype=='公共':
            sheet = self.gg_case_sheet
            test_start = self.gg_case_start
            test_end = self.gg_case_end
        elif testtype=='黑名单_客户评级B':
            sheet = self.phhmdB_case_sheet
            test_start = self.phhmdB_case_start
            test_end = self.phhmdB_case_end
        elif testtype=='黑名单_客户评级非B':
            sheet = self.phhmdO_case_sheet
            test_start = self.phhmdO_case_start
            test_end = self.phhmdO_case_end
        elif testtype=='黑名单_无客户评级':
            sheet = self.phhmd_case_sheet
            test_start = self.phhmd_case_start
            test_end = self.phhmd_case_end
        elif testtype=='普惠贷前':
            sheet = self.phdq_case_sheet
            test_start = self.phdq_case_start
            test_end = self.phdq_case_end
        elif testtype=='普惠贷中':
            sheet = self.phdz_case_sheet
            test_start = self.phdz_case_start
            test_end = self.phdz_case_end
        elif testtype=='SAAS-交易':
            sheet = self.saas_jy_case_sheet
            test_start = self.saas_jy_case_start
            test_end = self.saas_jy_case_end
        elif testtype=='SAAS-激活':
            sheet = self.saas_jh_case_sheet
            test_start = self.saas_jh_case_start
            test_end = self.saas_jh_case_end
        elif testtype=='SAAS-通用':
            sheet = self.saas_ty_case_sheet
            test_start = self.saas_ty_case_start
            test_end = self.saas_ty_case_end
        elif testtype=='SAAS-绑卡':
            sheet = self.saas_bk_case_sheet
            test_start = self.saas_bk_case_start
            test_end = self.saas_bk_case_end
        elif testtype=='SAAS-登陆':
            sheet = self.saas_dl_case_sheet
            test_start = self.saas_dl_case_start
            test_end = self.saas_dl_case_end
        elif testtype=='SAAS-注册':
            sheet = self.saas_zc_case_sheet
            test_start = self.saas_zc_case_start
            test_end = self.saas_zc_case_end

        elif testtype=='猛犸-交易':
            sheet = self.ma_jy_case_sheet
            test_start = self.ma_jy_case_start
            test_end = self.ma_jy_case_end
        elif testtype=='猛犸-激活':
            sheet = self.ma_jh_case_sheet
            test_start = self.ma_jh_case_start
            test_end = self.ma_jh_case_end
        elif testtype=='猛犸-通用':
            sheet = self.ma_ty_case_sheet
            test_start = self.ma_ty_case_start
            test_end = self.ma_ty_case_end
        elif testtype=='猛犸-绑卡':
            sheet = self.ma_bk_case_sheet
            test_start = self.ma_bk_case_start
            test_end = self.ma_bk_case_end
        elif testtype=='猛犸-登陆':
            sheet = self.ma_dl_case_sheet
            test_start = self.ma_dl_case_start
            test_end = self.ma_dl_case_end
        elif testtype=='猛犸-注册':
            sheet = self.ma_zc_case_sheet
            test_start = self.ma_zc_case_start
            test_end = self.ma_zc_case_end

        return (sheet,test_start,test_end)

    #批量同步关键key，并返回最终测试数据
    def gen_re_cache_list(self,testtype):

        #根据type选择对应的用例sheet页和起止行
        (sheet,test_start,test_end) = self.choose_sheet(testtype)

        if str(test_start)=='0' or str(test_end)=='0':
            test_data_list = [("None","None","None","None","None")]
        else:
            #获取code列
            expect_list = excel.colname_read_excel_return_list(testfile,sheet,\
                     self.expect_result_col,test_start, test_end)
            #获取table列
            table_list = excel.colname_read_excel_return_list(testfile,sheet,\
                     self.table_col,test_start, test_end)


            #获取关键key列，用于缓存删除
            key_list = excel.colname_read_excel_return_list(testfile,sheet,\
                     self.key_col,test_start, test_end)

            #获取remark列
            remark_list = excel.colname_read_excel_return_list(testfile,sheet,\
                     self.case_name_col,test_start, test_end)

            #生成request数据
            re_data_list = self.gen_re_data_list(sheet,test_start,test_end)

            #生成缓存数据
            cachedata_list = excel.colname_read_excel_return_list(testfile,sheet,\
                     self.cache_para_col,test_start, test_end)

            #处理request和cache的数据格式。
            # request数据格式要求[{}], 在此处做变更
            re_data_list_finall = []
            for re_data in re_data_list:
                tmplist = []
                if 'request' in re_data:
                    re_data_list_finall.append(re_data['request'])
                else:
                    tmplist.append(re_data)
                    re_data_list_finall.append(tmplist)

            #cache数据格式要求[{}, {}], 在此处做变更
            cachedata_list_finall = []

            for cache_data in cachedata_list:
                if str(cache_data)!='None':
                    cache_data =opera.transform_data(cache_data)
                    cache_data_one_list = []
                    for key in cache_data:
                        cache_data_one_list.append(cache_data[key])
                else:
                    cache_data_one_list = ['None']
                cachedata_list_finall.append(cache_data_one_list)

            #整合所有测试数据
            test_data_list = list(zip(expect_list,re_data_list_finall,cachedata_list_finall,remark_list,table_list))

        return test_data_list

    def gen_casename(self,testtype):
        #根据type选择对应的用例sheet页和起止行

        (sheet,test_start,test_end) = self.choose_sheet(testtype)
        if str(test_start)=='0' or str(test_end)=='0':
            expect_list = ["None"]
        else:
            expect_list = excel.colname_read_excel_return_list(testfile,sheet,\
                     self.expect_result_col,test_start, test_end)

        return expect_list

    def gen_deldata(self):
        deldata_list = [{"biz":"PAY.CREDIT","key":"udid-111666-wu","tag":"设备指纹"},\
                        {"biz":"PAY.CREDIT","key":"10.10.10.11-11","tag":"IP"},\
                        {"biz":"PAY.CREDIT","key":"auto-00-C9-4E-DE-7A-AA","tag":"MAC"},\
                        {"biz":"PAY.CREDIT","key":"auto-18911112222","tag":"手机号"},\
                        {"biz":"PAY.CREDIT","key":"44078219870520113X","tag":"身份证号"}]
        return deldata_list

if __name__ == '__main__':
    run = get_excel_data()
    a = run.gen_re_cache_list('通用')

    # modify_para_dict = {'a':'1','b':'2'}
    # print(modify_para_dict[0])
